package com.zhu.pre.controller;

import com.sanploy.common.domain.UserAddress;
import com.sanploy.common.vo.BaseException;
import com.zhu.pre.service.MUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by yingzhi zhu on 2017/9/26.
 * @author yingzhi zhu
 * 用户收货地址请求路由
 */
@RestController
@RequestMapping("/m/address")
public class MUserAddressController {


    @Autowired
    private MUserAddressService addressService;

    /**
     * 添加收货地址
     * 参数详见 UserAddress
     * @param userAddress
     * @param token
     * @param name
     * @param province
     * @param city
     * @param district
     * @param street
     * @param address
     * @param phone
     * @param city_id
     * @param district_id
     * @param province_id
     * @return
     * @throws BaseException
     */

    @PostMapping("")
    public Object postAddress(UserAddress userAddress,@RequestParam String token, @RequestParam(defaultValue = "") String name,@RequestParam String province,
                              @RequestParam String city,@RequestParam String district,@RequestParam(defaultValue = "") String street,@RequestParam String address,
                              @RequestParam String phone,@RequestParam String city_id,@RequestParam String district_id,@RequestParam String province_id,@RequestParam String code) throws BaseException {
        return addressService.addUserAddress(userAddress,token);
    }

    /**
     * 查看用户所有的地址 第一个为默认
     * @param token
     * @return
     * @throws BaseException
     */
    @GetMapping("")
    public Object getUserAddress(@RequestParam String token) throws BaseException {
        return addressService.getUserAddress(token);
    }

    /**
     * 查看用户的默认地址
     * @param token
     * @return
     * @throws BaseException
     */
    @GetMapping("/default")
    public Object getDefault(@RequestParam String token) throws BaseException {
        return addressService.getUserDefaultAddress(token);
    }

    /**
     * 修改某个收货地址为默认收货地址
     * @param token
     * @param id 收货地址id
     * @return
     * @throws BaseException
     */
    @PostMapping("/default")
    public Object setDefault(@RequestParam String token,@RequestParam Long id) throws BaseException {
        return addressService.setDefaultAddress(token,id);
    }

    /**
     * 删除某个地址
     * @param id
     * @param token
     * @return
     * @throws BaseException
     */
    @GetMapping("/delete/{id}")
    public Object deleteAddress(@PathVariable Long id,@RequestParam String token) throws BaseException {
        return addressService.deleteAddress(id,token);
    }

    /**
     * 编辑某个地址
     * 参数详见 UserAddress
     * @param userAddress
     * @param id
     * @param token
     * @param name
     * @param province
     * @param city
     * @param district
     * @param street
     * @param address
     * @param city_id
     * @param district_id
     * @param province_id
     * @param phone
     * @return
     * @throws BaseException
     */

    @PostMapping("/{id}")
    public Object UpdateAddress(UserAddress userAddress,@RequestParam String token,@PathVariable Long id,@RequestParam String name,
         @RequestParam String province,@RequestParam String city,@RequestParam String district,@RequestParam(defaultValue = "") String street,
         @RequestParam String address,@RequestParam String city_id,@RequestParam String code,@RequestParam String district_id,
                                @RequestParam String province_id,@RequestParam String phone
    ) throws BaseException {
        return addressService.updateAddress(userAddress,token);
    }

    /**
     * 查找某个地址
     * @param id 收货地址id
     * @param token
     * @return
     * @throws BaseException
     */

    @GetMapping("/{id}")
    public Object findOneAddress(@PathVariable Long id,@RequestParam String token) throws BaseException {
        return addressService.findOneAddress(id,token);
    }
}
